package com.apollographql.apollo.subscription;

import com.apollographql.apollo.api.internal.json.JsonReader;
import com.apollographql.apollo.api.internal.json.JsonWriter;
import com.apollographql.apollo.api.internal.json.ResponseJsonStreamReader;
import com.apollographql.apollo.api.internal.json.Utils;
import com.apollographql.apollo.subscription.OperationClientMessage;
import com.apollographql.apollo.subscription.OperationServerMessage;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import com.kt.Metadata;
import com.kt.collections.MapsKt;
import com.kt.jvm.internal.Intrinsics;

@Metadata(bv = {1, 0, 3}, d1 = {"\u0000\\\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\n\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0010$\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\bÆ\u0002\u0018\u00002\u00020\u0001B\u0007\b\u0002¢\u0006\u0002\u0010\u0002J\u0010\u0010\u000e\u001a\u00020\u000f2\u0006\u0010\u0010\u001a\u00020\u0011H\u0016J\u0018\u0010\u0012\u001a\u00020\u00132\u0006\u0010\u0014\u001a\u00020\u00152\u0006\u0010\u0016\u001a\u00020\u0017H\u0016J&\u0010\u0018\u001a\u000e\u0012\u0004\u0012\u00020\u0004\u0012\u0004\u0012\u00020\u001a0\u0019*\u0010\u0012\u0004\u0012\u00020\u0004\u0012\u0006\u0012\u0004\u0018\u00010\u001a0\u0019H\u0002J\f\u0010\u000e\u001a\u00020\u000f*\u00020\u001bH\u0002J\u0019\u0010\u001c\u001a\u00020\u0013*\u00020\u00152\u0006\u0010\u001d\u001a\u00020\u001eH\u0000¢\u0006\u0002\b\u001fJ\u0014\u0010\u001c\u001a\u00020\u0013*\u00020 2\u0006\u0010\u001d\u001a\u00020\u001eH\u0002J\u0014\u0010\u001c\u001a\u00020\u0013*\u00020!2\u0006\u0010\u001d\u001a\u00020\u001eH\u0002J\u0014\u0010\u001c\u001a\u00020\u0013*\u00020\"2\u0006\u0010\u001d\u001a\u00020\u001eH\u0002J\u0014\u0010\u001c\u001a\u00020\u0013*\u00020#2\u0006\u0010\u001d\u001a\u00020\u001eH\u0002J\u0019\u0010$\u001a\u00020\u0013*\u00020!2\u0006\u0010\u001d\u001a\u00020\u001eH\u0000¢\u0006\u0002\b%R\u000e\u0010\u0003\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0005\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0006\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u0007\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\b\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\t\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\n\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\u000b\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\f\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000R\u000e\u0010\r\u001a\u00020\u0004XT¢\u0006\u0002\n\u0000¨\u0006&"}, d2 = {"Lcom/apollographql/apollo/subscription/ApolloOperationMessageSerializer;", "Lcom/apollographql/apollo/subscription/OperationMessageSerializer;", "()V", "JSON_KEY_EXTENSIONS", "", "JSON_KEY_EXTENSIONS_PERSISTED_QUERY", "JSON_KEY_EXTENSIONS_PERSISTED_QUERY_HASH", "JSON_KEY_EXTENSIONS_PERSISTED_QUERY_VERSION", "JSON_KEY_ID", "JSON_KEY_OPERATION_NAME", "JSON_KEY_PAYLOAD", "JSON_KEY_QUERY", "JSON_KEY_TYPE", "JSON_KEY_VARIABLES", "readServerMessage", "Lcom/apollographql/apollo/subscription/OperationServerMessage;", "source", "Lokio/BufferedSource;", "writeClientMessage", "", "message", "Lcom/apollographql/apollo/subscription/OperationClientMessage;", "sink", "Lokio/BufferedSink;", "getMessagePayload", "", "", "Lcom/apollographql/apollo/api/internal/json/JsonReader;", "writeContentsTo", "writer", "Lcom/apollographql/apollo/api/internal/json/JsonWriter;", "writeContentsTo$apollo_runtime", "Lcom/apollographql/apollo/subscription/OperationClientMessage$Init;", "Lcom/apollographql/apollo/subscription/OperationClientMessage$Start;", "Lcom/apollographql/apollo/subscription/OperationClientMessage$Stop;", "Lcom/apollographql/apollo/subscription/OperationClientMessage$Terminate;", "writePayloadContentsTo", "writePayloadContentsTo$apollo_runtime", "apollo-runtime"}, k = 1, mv = {1, 4, 0})
/* compiled from: ApolloOperationMessageSerializer.kt */
public final class ApolloOperationMessageSerializer implements OperationMessageSerializer {
    public static final ApolloOperationMessageSerializer INSTANCE = new ApolloOperationMessageSerializer();
    public static final String JSON_KEY_EXTENSIONS = "extensions";
    public static final String JSON_KEY_EXTENSIONS_PERSISTED_QUERY = "persistedQuery";
    public static final String JSON_KEY_EXTENSIONS_PERSISTED_QUERY_HASH = "sha256Hash";
    public static final String JSON_KEY_EXTENSIONS_PERSISTED_QUERY_VERSION = "version";
    public static final String JSON_KEY_ID = "id";
    public static final String JSON_KEY_OPERATION_NAME = "operationName";
    public static final String JSON_KEY_PAYLOAD = "payload";
    public static final String JSON_KEY_QUERY = "query";
    public static final String JSON_KEY_TYPE = "type";
    public static final String JSON_KEY_VARIABLES = "variables";

    private ApolloOperationMessageSerializer() {
    }

    /* JADX WARNING: Code restructure failed: missing block: B:10:0x0037, code lost:
        throw r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:8:0x0033, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:9:0x0034, code lost:
        com.kt.io.CloseableKt.closeFinally(r0, r1);
     */
    @Override // com.apollographql.apollo.subscription.OperationMessageSerializer
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void writeClientMessage(OperationClientMessage r10, okio.BufferedSink r11) throws java.io.IOException {
        /*
            r9 = this;
            java.lang.String r0 = "message"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r10, r0)
            java.lang.String r0 = "sink"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r11, r0)
            com.apollographql.apollo.api.internal.json.JsonWriter$Companion r0 = com.apollographql.apollo.api.internal.json.JsonWriter.Companion
            com.apollographql.apollo.api.internal.json.JsonWriter r0 = r0.of(r11)
            java.io.Closeable r0 = (java.io.Closeable) r0
            r1 = 0
            java.lang.Throwable r1 = (java.lang.Throwable) r1
            r2 = r0
            com.apollographql.apollo.api.internal.json.JsonWriter r2 = (com.apollographql.apollo.api.internal.json.JsonWriter) r2     // Catch:{ all -> 0x0031 }
            r3 = 0
            r4 = r2
            r5 = 0
            r4.beginObject()     // Catch:{ all -> 0x0031 }
            r6 = r4
            r7 = 0
            com.apollographql.apollo.subscription.ApolloOperationMessageSerializer r8 = com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.INSTANCE     // Catch:{ all -> 0x0031 }
            r8.writeContentsTo$apollo_runtime(r10, r2)     // Catch:{ all -> 0x0031 }
            r4.endObject()     // Catch:{ all -> 0x0031 }
            com.kt.Unit r2 = com.kt.Unit.INSTANCE     // Catch:{ all -> 0x0031 }
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            return
        L_0x0031:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x0033 }
        L_0x0033:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            throw r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.writeClientMessage(com.apollographql.apollo.subscription.OperationClientMessage, okio.BufferedSink):void");
    }

    /* JADX WARNING: Code restructure failed: missing block: B:13:0x0033, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:14:0x0034, code lost:
        com.kt.io.CloseableKt.closeFinally(r5, r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:15:0x0037, code lost:
        throw r2;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:19:0x003a, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:20:0x003b, code lost:
        com.kt.io.CloseableKt.closeFinally(r0, r1);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:21:0x003e, code lost:
        throw r2;
     */
    @Override // com.apollographql.apollo.subscription.OperationMessageSerializer
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public OperationServerMessage readServerMessage(okio.BufferedSource r11) throws java.io.IOException {
        /*
            r10 = this;
            java.lang.String r0 = "source"
            com.kt.jvm.internal.Intrinsics.checkParameterIsNotNull(r11, r0)
            okio.BufferedSource r0 = r11.peek()     // Catch:{ JsonEncodingException -> 0x0052, IOException -> 0x004d, Exception -> 0x003f }
            java.io.Closeable r0 = (java.io.Closeable) r0     // Catch:{ JsonEncodingException -> 0x0052, IOException -> 0x004d, Exception -> 0x003f }
            r1 = 0
            r2 = r1
            java.lang.Throwable r2 = (java.lang.Throwable) r2     // Catch:{ JsonEncodingException -> 0x0052, IOException -> 0x004d, Exception -> 0x003f }
            r3 = r0
            okio.BufferedSource r3 = (okio.BufferedSource) r3     // Catch:{ all -> 0x0038 }
            r4 = 0
            com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader r5 = new com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader     // Catch:{ all -> 0x0038 }
            r5.<init>(r3)     // Catch:{ all -> 0x0038 }
            java.io.Closeable r5 = (java.io.Closeable) r5     // Catch:{ all -> 0x0038 }
            java.lang.Throwable r1 = (java.lang.Throwable) r1     // Catch:{ all -> 0x0038 }
            r6 = r5
            com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader r6 = (com.apollographql.apollo.api.internal.json.BufferedSourceJsonReader) r6     // Catch:{ all -> 0x0031 }
            r7 = 0
            com.apollographql.apollo.subscription.ApolloOperationMessageSerializer r8 = com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.INSTANCE     // Catch:{ all -> 0x0031 }
            r9 = r6
            com.apollographql.apollo.api.internal.json.JsonReader r9 = (com.apollographql.apollo.api.internal.json.JsonReader) r9     // Catch:{ all -> 0x0031 }
            com.apollographql.apollo.subscription.OperationServerMessage r8 = r8.readServerMessage(r9)     // Catch:{ all -> 0x0031 }
            com.kt.io.CloseableKt.closeFinally(r5, r1)
            com.kt.io.CloseableKt.closeFinally(r0, r2)
            goto L_0x005f
        L_0x0031:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x0033 }
        L_0x0033:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r5, r1)
            throw r2
        L_0x0038:
            r1 = move-exception
            throw r1     // Catch:{ all -> 0x003a }
        L_0x003a:
            r2 = move-exception
            com.kt.io.CloseableKt.closeFinally(r0, r1)
            throw r2
        L_0x003f:
            r0 = move-exception
            com.apollographql.apollo.subscription.OperationServerMessage$Unsupported r1 = new com.apollographql.apollo.subscription.OperationServerMessage$Unsupported
            java.lang.String r2 = r11.readUtf8()
            r1.<init>(r2)
            r8 = r1
            com.apollographql.apollo.subscription.OperationServerMessage r8 = (com.apollographql.apollo.subscription.OperationServerMessage) r8
            goto L_0x005f
        L_0x004d:
            r0 = move-exception
            r1 = r0
            java.lang.Throwable r1 = (java.lang.Throwable) r1
            throw r1
        L_0x0052:
            r0 = move-exception
            com.apollographql.apollo.subscription.OperationServerMessage$Unsupported r1 = new com.apollographql.apollo.subscription.OperationServerMessage$Unsupported
            java.lang.String r2 = r11.readUtf8()
            r1.<init>(r2)
            r8 = r1
            com.apollographql.apollo.subscription.OperationServerMessage r8 = (com.apollographql.apollo.subscription.OperationServerMessage) r8
        L_0x005f:
            return r8
        */
        throw new UnsupportedOperationException("Method not decompiled: com.apollographql.apollo.subscription.ApolloOperationMessageSerializer.readServerMessage(okio.BufferedSource):com.apollographql.apollo.subscription.OperationServerMessage");
    }

    public final void writeContentsTo$apollo_runtime(OperationClientMessage $this$writeContentsTo, JsonWriter writer) {
        Intrinsics.checkParameterIsNotNull($this$writeContentsTo, "$this$writeContentsTo");
        Intrinsics.checkParameterIsNotNull(writer, "writer");
        if ($this$writeContentsTo instanceof OperationClientMessage.Init) {
            writeContentsTo((OperationClientMessage.Init) $this$writeContentsTo, writer);
        } else if ($this$writeContentsTo instanceof OperationClientMessage.Start) {
            writeContentsTo((OperationClientMessage.Start) $this$writeContentsTo, writer);
        } else if ($this$writeContentsTo instanceof OperationClientMessage.Stop) {
            writeContentsTo((OperationClientMessage.Stop) $this$writeContentsTo, writer);
        } else if ($this$writeContentsTo instanceof OperationClientMessage.Terminate) {
            writeContentsTo((OperationClientMessage.Terminate) $this$writeContentsTo, writer);
        }
    }

    private final void writeContentsTo(OperationClientMessage.Init $this$writeContentsTo, JsonWriter writer) {
        try {
            writer.name("type").value(OperationClientMessage.Init.TYPE);
            if (!$this$writeContentsTo.connectionParams.isEmpty()) {
                writer.name("payload");
                Utils.writeToJson($this$writeContentsTo.connectionParams, writer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private final void writeContentsTo(OperationClientMessage.Start $this$writeContentsTo, JsonWriter writer) {

        try {
            writer.name("id").value($this$writeContentsTo.subscriptionId);
            writer.name("type").value(OperationClientMessage.Start.TYPE);
            JsonWriter $this$writeObject$iv = writer.name("payload");
            $this$writeObject$iv.beginObject();
            INSTANCE.writePayloadContentsTo$apollo_runtime($this$writeContentsTo, writer);
            if ($this$writeContentsTo.autoPersistSubscription) {
                JsonWriter $this$writeObject$iv2 = $this$writeObject$iv.name(JSON_KEY_EXTENSIONS);
                $this$writeObject$iv2.beginObject();
                JsonWriter $this$writeObject$iv3 = $this$writeObject$iv2.name(JSON_KEY_EXTENSIONS_PERSISTED_QUERY);
                $this$writeObject$iv3.beginObject();
                $this$writeObject$iv3.name(JSON_KEY_EXTENSIONS_PERSISTED_QUERY_VERSION).value(1L);
                $this$writeObject$iv3.name(JSON_KEY_EXTENSIONS_PERSISTED_QUERY_HASH).value($this$writeContentsTo.subscription.operationId());
                $this$writeObject$iv3.endObject();
                $this$writeObject$iv2.endObject();
            }
            $this$writeObject$iv.endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public final void writePayloadContentsTo$apollo_runtime(OperationClientMessage.Start $this$writePayloadContentsTo, JsonWriter writer) {
        Intrinsics.checkParameterIsNotNull($this$writePayloadContentsTo, "$this$writePayloadContentsTo");
        Intrinsics.checkParameterIsNotNull(writer, "writer");
        try {
            writer.name(JSON_KEY_VARIABLES).jsonValue($this$writePayloadContentsTo.subscription.variables().marshal($this$writePayloadContentsTo.scalarTypeAdapters));
            writer.name(JSON_KEY_OPERATION_NAME).value($this$writePayloadContentsTo.subscription.name().name());
            if (!$this$writePayloadContentsTo.autoPersistSubscription || $this$writePayloadContentsTo.sendSubscriptionDocument) {
                writer.name(JSON_KEY_QUERY).value($this$writePayloadContentsTo.subscription.queryDocument());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private final void writeContentsTo(OperationClientMessage.Stop $this$writeContentsTo, JsonWriter writer) {
        try {
            writer.name("id").value($this$writeContentsTo.subscriptionId);
            writer.name("type").value(OperationClientMessage.Stop.TYPE);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private final void writeContentsTo(OperationClientMessage.Terminate $this$writeContentsTo, JsonWriter writer) {
        try {
            writer.name("type").value(OperationClientMessage.Terminate.TYPE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private final OperationServerMessage readServerMessage(JsonReader $this$readServerMessage) {
        String type;
        Map messageData = null;
        try {
            messageData = new ResponseJsonStreamReader($this$readServerMessage).toMap();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (messageData != null) {
            String id = (String) messageData.get("id");
            String str = (String) messageData.get("type");
            if (str != null) {
                type = str;
                switch (str.hashCode()) {
                    case -599445191:
                        if (str.equals(OperationServerMessage.Complete.TYPE)) {
                            return new OperationServerMessage.Complete(id);
                        }
                        break;
                    case 3414:
                        if (str.equals(OperationServerMessage.ConnectionKeepAlive.TYPE)) {
                            return new OperationServerMessage.ConnectionKeepAlive();
                        }
                        break;
                    case 3076010:
                        if (str.equals(OperationServerMessage.Data.TYPE)) {
                            return new OperationServerMessage.Data(id, getMessagePayload(messageData));
                        }
                        break;
                    case 96784904:
                        if (str.equals(OperationServerMessage.Error.TYPE)) {
                            return new OperationServerMessage.Error(id, getMessagePayload(messageData));
                        }
                        break;
                    case 1198953831:
                        if (str.equals(OperationServerMessage.ConnectionError.TYPE)) {
                            return new OperationServerMessage.ConnectionError(getMessagePayload(messageData));
                        }
                        break;
                    case 1270515624:
                        if (str.equals(OperationServerMessage.ConnectionAcknowledge.TYPE)) {
                            return new OperationServerMessage.ConnectionAcknowledge();
                        }
                        break;
                }
            } else {
                type = str;
            }
            throw new IllegalArgumentException("Unsupported message type " + type);
        }
        throw new IllegalArgumentException("Required value was null.".toString());
    }

    private final Map<String, Object> getMessagePayload(Map<String, ? extends Object> map) {
        Map it;
        Map it2 = (Map) map.get("payload");
        return (it2 == null || (it = Collections.unmodifiableMap(it2)) == null) ? MapsKt.emptyMap() : it;
    }
}
